package com.syn.michat.controller;

import com.alibaba.fastjson.JSON;
import com.syn.michat.constant.FriendContants;
import com.syn.michat.model.ResponseEntity;
import com.syn.michat.pojo.Friend;
import com.syn.michat.pojo.User;
import com.syn.michat.service.FriendsService;
import com.syn.michat.service.UserService;
import com.syn.michat.util.ResponseErrorUtil;
import com.syn.michat.util.TokenUtil;
import com.syn.michat.util.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping(produces = "application/json;charset=UTF-8")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FriendsService friendsService;

    @RequestMapping("/status")
    public HashMap<String, String> emailNotify() {
        HashMap<String, String> map = new HashMap();
        map.put("status", "1");
        return map;
    }

    @PostMapping("/account/login")
    public String login(@RequestParam String username, @RequestParam String password) {
        try {
            User user = userService.findUser(username,password);
            TokenUtil util = new TokenUtil();
            String token = util.getToken(user.getUserId(), user.getUsername());

            HashMap<String, Object> map = new HashMap();
            map.put("token", token);
            map.put("user", user);

            ResponseEntity entity = new ResponseEntity(map);
            return JSON.toJSONString(entity);
        } catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }

    @PostMapping("/account/logout")
    public String logout(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");

        // TODO: 注销socket

        return JSON.toJSONString(new ResponseEntity());
    }

    @PostMapping("/account/register")
    public String register(@RequestParam String username, @RequestParam String password) {
        String uuid = UUID.randomUUID().toString().replaceAll("-","");

        Random rand = new Random();
        String avatar = rand.nextInt(10) + ".jpeg";

        User user = new User();
        user.setUserId(uuid);
        user.setUsername(username);
        user.setNickname(username);
        user.setPassword(password);
        user.setAvatar(avatar);

        try {
            userService.add(user);
            return JSON.toJSONString(new ResponseEntity());
        } catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }

    @GetMapping("/user/search")
    public String search(HttpServletRequest request, @RequestParam String username) {
        try {
            User user = (User) request.getAttribute("user");
            User searchUser = userService.search(username);
            if (searchUser == null) {
                return JSON.toJSONString(new ResponseEntity());
            }

            Friend friend = friendsService.findFriend(user.getUserId(), searchUser.getUserId());
            searchUser.setFriend(false);
            if (friend != null) {
                if (friend.getStatus() == FriendContants.FRIEND) {
                    searchUser.setFriend(true);
                }
            }

            HashMap<String, Object> map = new HashMap();
            map.put("user", searchUser);
            return JSON.toJSONString(new ResponseEntity(map));
        } catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }

    @PostMapping("/user/nickname")
    public String updateNickname(HttpServletRequest request, @RequestParam String nickname) {
        try {
            User user = (User) request.getAttribute("user");
            userService.updateNickname(user.getUsername(), nickname);
            return JSON.toJSONString(new ResponseEntity());
        } catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }
}
